package com.xyx.ssm.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xyx.ssm.po.Syuserrole;
import com.xyx.ssm.po.WineBrand;
import com.xyx.ssm.po.purchaseordersub;
import com.xyx.ssm.service.PurchaseorderService;
import com.xyx.ssm.util.Page;

@Controller
@RequestMapping("purchaseorder")
public class PurchaseorderAction {
	@Autowired
	private PurchaseorderService purchaseorderservice;
	
	@RequestMapping("purchaseorder")
	public String purchaseorder(){
		
		return "purchaseorder/purchaseorder";
	}
	
	
	
	@RequestMapping("/jsons")
	@ResponseBody
	public Map<String, Object> jsons(Model model, Integer iDisplayLength,
			Integer sEcho, Integer iDisplayStart, String pageName,
			String departName) {

		Map<String, Object> map111 = new HashMap<String, Object>();
		// 用户名

		map111.put("pageName", "");
		if (sEcho == null) {
			sEcho = 1;
		}
		if (iDisplayLength == null) {
			iDisplayLength = 10;
		}
		if (iDisplayStart == null) {
			iDisplayStart = 1;
		}
		System.out.println(map111.toString());
		// 1:当前页码

		// 1:查询参数

		Page<Map<String, Object>> page = new Page<Map<String, Object>>(
				iDisplayStart, map111, iDisplayLength);
		Page<Map<String, Object>> page1 = purchaseorderservice.selallPurchaseorder(page);
		int num = page1.getList().size();
		Map<String, Object> map = new HashMap<String, Object>();
		List<Ceshi> list = new ArrayList<Ceshi>();
		for (int i = 0; i < num; i++) {
			Ceshi ceshi = new Ceshi();
			ceshi.setId(i);
			ceshi.setUsername(page1.getList().get(i).get("id").toString());
			list.add(ceshi);
		}
		map.put("sEcho", sEcho);
		map.put("iTotalRecords", page1.getTotalCount());
		map.put("iTotalDisplayRecords", page1.getTotalCount());
		map.put("aaData", page1.getList());
		return map;
	}
/**
 * 查询purchaseordersub表
 */
	@RequestMapping("/selAurchaseordersub")
	@ResponseBody
	public Map<String, Object> selAurchaseordersub(Integer id){
		System.out.println(id);
		Map<String, Object> list=purchaseorderservice.selpurchaseordersub(id);		
		return list;		
	}
	/**
	 * 看订单中的酒
	 */
	@RequestMapping("/selwine")
	@ResponseBody
	public List<Map<String,Object>> selwine(Integer phoid){
		List<Map<String, Object>> list=purchaseorderservice.selwine(phoid);
		System.out.println(list.toString()+"+++++++++++++++++++++++++++");
		return list;		
	}
	@RequestMapping("/jsonwinebrand")
	@ResponseBody
	public List<Map<String, Object>> jsonwinebrand(){
		
		List<Map<String, Object>> list=purchaseorderservice.selallwine();
		
		
				return list;
	}
	@RequestMapping("/jsonwinebrand1111")
	public String addwine(HttpServletRequest request){
		String[] ids=request.getParameterValues("ids");
		String phoid=request.getParameter("phoid");
		String phid=request.getParameter("phid");
		String uid=request.getParameter("uid");
		int num=ids.length;
		for(String id:ids){
			String n=request.getParameter("num"+id);
			System.out.println(id+"???");
			
			List<purchaseordersub> list=purchaseorderservice.selwine111();
			for(purchaseordersub pur:list){
				
				
			if(Integer.parseInt(id)==pur.getWid()){
				
				purchaseordersub p1=new purchaseordersub();
				int num3=Integer.parseInt(n)+pur.getNum();
				System.out.println("num3="+num3);
				pur.setNum(num3);
				int num1=purchaseorderservice.updatewinenum(pur);
				System.out.println("num1="+num1);
			}
			else{
				purchaseordersub p1=new purchaseordersub();
				p1.setNum(Integer.parseInt(n)+0);
				p1.setPhoid(Integer.parseInt(phoid));
				p1.setPhid(Integer.parseInt(phid));
				p1.setUid(Integer.parseInt(uid));
				p1.setWid(Integer.parseInt(id));
				int num2=purchaseorderservice.insertpurchaseordersub(p1);
				System.out.println("num2="+num2);
			}}
			}
		
		
		
		return "purchaseorder/purchaseorder";
	}
	
	
}
